/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.manage.service;

import info.peigen.hotpot.common.core.result.PageResult;
import info.peigen.hotpot.manage.core.manage.convertor.OrganizationManageConvertor;
import info.peigen.hotpot.manage.core.manage.form.OrganizationCreateForm;
import info.peigen.hotpot.manage.core.manage.form.OrganizationEditForm;
import info.peigen.hotpot.manage.core.manage.form.OrganizationSearchForm;
import info.peigen.hotpot.manage.core.manage.repository.service.OrganizationManageRepository;
import info.peigen.hotpot.manage.core.repository.entity.OrganizationEntity;
import info.peigen.hotpot.manage.core.service.OrganizationService;
import info.peigen.hotpot.manage.core.service.order.OrganizationCreateOrder;
import info.peigen.hotpot.manage.core.service.order.OrganizationEditOrder;
import info.peigen.hotpot.manage.core.service.result.OrganizationCreateResult;
import info.peigen.hotpot.manage.core.service.result.OrganizationEditResult;
import info.peigen.hotpot.manage.core.service.result.info.OrganizationInfo;
import org.beetl.sql.core.query.LambdaQuery;
import org.beetl.sql.core.query.Query;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.List;

/**
 * <b>(OrganizationManageService)</b>
 * 管理后台 "组织机构" 服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-08-05
 */
@SuppressWarnings("unused")
@Component
public class OrganizationManageService {
    @Inject
    OrganizationManageRepository organizationManageRepository;
    @Inject
    OrganizationService          organizationService;

    public PageResult<OrganizationInfo> page(OrganizationSearchForm form) {
        LambdaQuery<OrganizationEntity> lambdaQuery = query(form);
        return organizationManageRepository.page(form.getPageNum(), form.getPageSize(), lambdaQuery, OrganizationManageConvertor.INSTANCE::convertToInfo);
    }

    public List<OrganizationInfo> list(OrganizationSearchForm form) {
        LambdaQuery<OrganizationEntity> lambdaQuery = query(form);
        List<OrganizationEntity>        entities    = lambdaQuery.select();
        return entities.stream().map(OrganizationManageConvertor.INSTANCE::convertToInfo).toList();
    }

    public OrganizationInfo create(OrganizationCreateForm form) {
        OrganizationCreateOrder  order  = OrganizationManageConvertor.INSTANCE.convertToCreateOrder(form);
        OrganizationCreateResult result = organizationService.create(order);
        if (result.success()) {
            return result.getInfo();
        }
        return null;
    }

    public OrganizationInfo edit(OrganizationEditForm form) {
        OrganizationEditOrder  order  = OrganizationManageConvertor.INSTANCE.convertToEditOrder(form);
        OrganizationEditResult result = organizationService.edit(order);
        if (result.success()) {
            return result.getInfo();
        }
        return null;
    }

    public void remove(Integer id) {
        organizationManageRepository.delete(id);
    }

    public void removes(List<Integer> ids) {
        organizationManageRepository.removeByIds(ids);
    }

    public OrganizationInfo detail(Integer id) {
        OrganizationEntity entity = organizationManageRepository.getMapper().single(id);
        return OrganizationManageConvertor.INSTANCE.convertToInfo(entity);
    }

    public OrganizationInfo detail(String organizationId) {
        OrganizationEntity entity = organizationManageRepository.getMapper().findByOrganizationId(organizationId);
        return OrganizationManageConvertor.INSTANCE.convertToInfo(entity);
    }

    private LambdaQuery<OrganizationEntity> query(OrganizationSearchForm form) {
        return organizationManageRepository.getMapper().createLambdaQuery()
                .andLike(OrganizationEntity::getOrganizationId, Query.filterLikeEmpty(form.getOrganizationId(), true, true))
                .andLike(OrganizationEntity::getPartnerId, Query.filterLikeEmpty(form.getPartnerId(), true, true))
                .desc(form.getOrderName())
                ;
    }

}